<?php

declare(strict_types=1);

namespace owns\library;

use think\facade\Config;

class Func
{
    const PI = 3.14159; // 圆周率
    /**
     * 生成单号
     * @param string|int $prefix 前缀
     */
    public static function generateNumber($prefix = ''): string
    {
        return $prefix . date('YmdHis') . str_pad((string) mt_rand(1, 9999), 4, '0', STR_PAD_LEFT);
    }
    /**
     * 计算两组经纬度坐标之间的距离
     * @param float $lng1 经度1
     * @param float $lat1 纬度1
     * @param float $lng2 经度2
     * @param float $lat2 纬度2
     * @param boolean $fmt 返回格式：true 带单位 false 不带
     * @param integer $len_type （1:m or 2:km)
     * @param integer $decimal
     * @return string|int
     */
    public static function getDistance($lng1, $lat1, $lng2, $lat2, $fmt = false, $len_type = 2, $decimal = 2)
    {
        $earthRadius = 6378.137; // 地球平均半径
        $radLat1 = $lat1 * self::PI / 180;
        $radLat2 = $lat2 * self::PI / 180;
        $a = $radLat1 - $radLat2;
        $b = ($lng1 * self::PI / 180) - ($lng2 * self::PI / 180);
        $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
        $s = $s * $earthRadius;
        $s = round($s * 1000);
        if ($fmt) {
            return $s > 1000 ? ($s / 1000) . 'km' : $s . 'm';
        } else {
            if ($len_type > 1) {
                $s /= 1000;
            }
            return round($s, $decimal);
        }
    }
    /**
     * sql查询距离计算
     * @param string lng 经度
     * @param string lat 纬度
     * @param string alias 表别名
     * @param int distance 查询距离,默认5km
     */
    public static function distanceSqrt($lng, $lat, $alias = '', $distance = null): string
    {
        $PI = self::PI;
        $distance = $distance ?? 5;
        if ($alias) {
            $alias .= '.';
        }
        $sql = "sqrt(((({$lng}-{$alias}lng)*{$PI}*12656*cos((({$lat}+{$alias}lat)/2)*{$PI}/180)/180)";
        $sql .= "*(({$lng}-{$alias}lng)*{$PI}*12656*cos((({$lat}+{$alias}lat)/2)*{$PI}/180)/180))";
        $sql .= "+((({$lat}-{$alias}lat)*{$PI}*12656/180) * (({$lat}-{$alias}lat)*{$PI}*12656/180)))";
        $sql .= "<{$distance}";
        return $sql;
    }
    /**
     * sql距离排行搜索 - 计算单位 km
     * @param string $lng 经度
     * @param string $lat 纬度
     * @param string $alias 表别名
     * @param string $sort 排序
     */
    public static function distanceSort($lng, $lat, $alias = ''): string
    {
        $PI = self::PI;
        if ($alias) {
            $alias .= '.';
        }
        $sql = "acos(sin(({$lat}*{$PI})/180)*sin(({$alias}lat*{$PI})/180)";
        $sql .= "+cos(({$lat}*{$PI})/180)*cos(({$alias}lat*{$PI})/180)";
        $sql .= "*cos(({$lng}*{$PI})/180-({$alias}lng*{$PI})/180))*6380";
        return $sql;
    }

    /**
     * 随机码
     */
    public static function randomCode($code_text = 'mixture', $code_len = 8): string
    {
        switch ($code_text) {
            case 'mixture':
                $num = 61;
                $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
                break;
            case 'capital':
                $num = 25;
                $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
                break;
            case 'lowercase':
                $num = 25;
                $chars = 'abcdefghijklmnopqrstuvwxyz';
                break;
            case 'number':
                $num = 9;
                $chars = '0123456789';
                break;
            default:
                tips('不支持该类型随机码');
        }
        $code = '';
        for ($i = 0; $i < $code_len; $i++) {
            $code .= $chars[mt_rand(0, $num)];
        }
        return $code;
    }
    /**
     * 删除文件
     * @param string $path
     */
    public static function deleteFile(string $path): void
    {
        $default = Config::get('filesystem.default');
        $list = explode(',', $path);
        foreach ($list as $file) {
            if ($default == 'local') {
                @unlink(app()->getRuntimePath() . $file);
            } else if ($default == 'public') {
                @unlink(app()->getRootPath() . 'public/' . $file);
            }
        }
    }
}
